﻿using System;
using System.Drawing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using IWshRuntimeLibrary;
using IceStart.View;
using IceStart.Core;
using IceStart.Utils;
using System.Windows.Media.Effects;

namespace IceStart
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Win32API.AddGlobalHotkey((s, e) =>
            {
                if (Application.Current.MainWindow.WindowState != WindowState.Minimized)
                {
                    HideWindow();
                }
                else
                {
                    ShowWindow();
                }
            }, "E");
            DataHandler.LoadMagnet();
        }

        protected override void OnStateChanged(EventArgs e)
        {
            base.OnStateChanged(e);
            if (WindowState != WindowState.Minimized)
            {
                ShowWindow();
            }
        }

        #region 窗口行为

        public void HideWindow()
        {
            DoubleAnimation animation = new DoubleAnimation(1, 0, TimeSpan.FromSeconds(0.08));
            animation.Completed += (s, e) =>
            {
                WindowState = WindowState.Minimized;
                GC.Collect();
            };
            MainWindow_Grid.BeginAnimation(OpacityProperty, animation);
        }

        public void ShowWindow()
        {
            DoubleAnimation animation = new DoubleAnimation(0, 1, TimeSpan.FromSeconds(0.08));
            animation.Completed += (s, e) =>
            {
                WindowState = WindowState.Normal;
                GC.Collect();
            };

            MainWindow_Grid.BeginAnimation(OpacityProperty, animation);
        }

        #endregion 窗口行为

        #region 关闭窗口按键的行为

        private object? CloseWindow_ButtonCache = null;

        private void CloseWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            CloseWindow_ButtonCache = sender;
            e.Handled = true;
        }

        private void CloseWindow_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (CloseWindow_ButtonCache != null)
            {
                CloseWindow_ButtonCache = null;
                HideWindow();
            }
        }

        private void CloseWindow_MouseEnter(object sender, MouseEventArgs e)
        {
            CloseWindow_Border.Background = (SolidColorBrush)Application.Current.Resources["MainColor_Selected"];
        }

        private void CloseWindow_MouseLeave(object sender, MouseEventArgs e)
        {
            CloseWindow_Border.Background = (SolidColorBrush)Application.Current.Resources["Transprant"];
        }

        #endregion 关闭窗口按键的行为

        #region 窗口移动

        private void TopBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }

        #endregion 窗口移动

        #region 编辑菜单栏

        private object? Edit_ButtonCache = null;

        private void Edit_Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Edit_ButtonCache = sender;
            e.Handled = true;
        }

        private void Edit_Button_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (Edit_ButtonCache != null)
            {
                Edit_ButtonCache = null;
                if (((TextBlock)Application.Current.Resources["Theme_Name"]).Text == "Light")
                {
                    var theme = Application.LoadComponent(
                        new Uri(@"Theme/Sky_Theme.xaml", UriKind.Relative)) as ResourceDictionary;
                    this.Resources.MergedDictionaries.Clear();
                    this.Resources.MergedDictionaries.Add(theme);
                    Application.Current.Resources = theme;
                }
                else
                {
                    var theme = Application.LoadComponent(
                        new Uri(@"Theme/Light_Theme.xaml", UriKind.Relative)) as ResourceDictionary;
                    this.Resources.MergedDictionaries.Clear();
                    this.Resources.MergedDictionaries.Add(theme);
                    Application.Current.Resources = theme;
                }
            }
            WindowState = WindowState.Maximized;
        }

        private void Edit_Button_MouseEnter(object sender, MouseEventArgs e)
        {
            Edit_Border.Background = (SolidColorBrush)Application.Current.Resources["MainColor_Selected"];
        }

        private void Edit_Button_MouseLeave(object sender, MouseEventArgs e)
        {
            Edit_Border.Background = (SolidColorBrush)Application.Current.Resources["Transprant"];
        }

        #endregion 编辑菜单栏

        #region 查找

        private void Search_Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
        }

        private void Search_Button_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
        }

        private void Search_Button_MouseEnter(object sender, MouseEventArgs e)
        {
            Search_Border.Background = (SolidColorBrush)Application.Current.Resources["MainColor_Selected"];
        }

        private void Search_Button_MouseLeave(object sender, MouseEventArgs e)
        {
            Search_Border.Background = (SolidColorBrush)Application.Current.Resources["Transprant"];
        }

        #endregion 查找

        #region 拖动放入文件

        private void MainWindow_Grid_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string path = ((Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0)!.ToString()!;
                AddFileMagnet(path, AbstractMagnetTie.Size.Large, e);
            }
        }

        private void AddFileMagnet(string path, AbstractMagnetTie.Size size, DragEventArgs e)
        {
            string originPath = path;
            if (path.EndsWith("lnk"))
            {
                IWshShortcut shortcut = (IWshShortcut)(new WshShell().CreateShortcut(path));
                path = shortcut.TargetPath;
            }
            Icon ico = Win32API.GetFileIcon(path)!;

            FileMagnetTie magnetTie = new FileMagnetTie(path);
            magnetTie.ButtonSize = AbstractMagnetTie.Size.Small;
            FileMagnet magnet = new FileMagnet(magnetTie);
            magnet.FileIcon.Source = Imaging.CreateBitmapSourceFromHBitmap(
                    ico.ToBitmap().GetHbitmap(),
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());
            magnet.FileName.Text = GenericUtils.GetFileName(originPath);
            Coordinates.RegisterPosition(magnet, e.GetPosition(MainMagnet_Container));
            MainMagnet_Container.Children.Add(magnet);
            ico.Dispose();

            GC.Collect();
            //MessageBox.Show(Canvas.GetTop(magnet).ToString());
        }

        public void AddFileMagnet(DataHandler.MagnetInfo info)
        {
            string path = info.Path;
            Icon ico = Win32API.GetFileIcon(path)!;

            FileMagnetTie magnetTie = new FileMagnetTie(path);
            magnetTie.ButtonSize = info.Size;
            FileMagnet magnet = new FileMagnet(magnetTie);
            magnet.FileIcon.Source = Imaging.CreateBitmapSourceFromHBitmap(
                    ico.ToBitmap().GetHbitmap(),
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());
            magnet.FileName.Text = info.Name;
            magnet.SetPosition(info.Top, info.Left);
            Coordinates.RegisterPosition(magnet);
            MainMagnet_Container.Children.Add(magnet);
            ico.Dispose();

            GC.Collect();
        }

        #endregion 拖动放入文件
    }
}